package com.xuxin.baby.popup;

import android.content.Context;
import android.text.TextPaint;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.xuxin.baby.R;
import com.xuxin.baby.utils.RecycleUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xu_xin
 * Created on 2020/12/18 0018
 */
public class GestationalPopup extends CenterPopupView {
    private OnClickListener onClickListener;
    private WeekAdapter weekAdapter;
    private DayAdapter dayAdapter;
    private String week = "40周", day;

    public GestationalPopup(@NonNull Context context) {
        super(context);
    }

    public GestationalPopup(@NonNull Context context, OnClickListener onClickListener) {
        super(context);
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onGestational(String week, String day);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_gestational;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        RecyclerView popup_gestational_week = findViewById(R.id.popup_gestational_week);
        RecyclerView popup_gestational_day = findViewById(R.id.popup_gestational_day);
        RecycleUtils.init(popup_gestational_week);
        RecycleUtils.init(popup_gestational_day);
        weekAdapter = new WeekAdapter();
        dayAdapter = new DayAdapter();
        popup_gestational_week.setAdapter(weekAdapter);
        popup_gestational_day.setAdapter(dayAdapter);
        weekAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                try {
                    LogUtils.e("position = " + position);
                    week = weekAdapter.getData().get(position);
                    LogUtils.e("week = " + week);
                    weekAdapter.refresh(position);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        dayAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                try {
                    day = dayAdapter.getData().get(position);
                    if (null != onClickListener && !StringUtils.isEmpty(week) && !StringUtils.isEmpty(day)) {
                        onClickListener.onGestational(week, day);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    dismiss();
                }
            }
        });
    }

    @Override
    protected int getMaxWidth() {
        return (int) (ScreenUtils.getScreenWidth() * 0.75);
    }

    @Override
    protected int getMaxHeight() {
        return (int) (ScreenUtils.getScreenWidth());
    }

    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    protected int getPopupWidth() {
        return 0;
    }

    protected int getPopupHeight() {
        return 0;
    }

    public static class WeekAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        private int position = 4;

        public WeekAdapter() {
            super(R.layout.item_gestational);
            List<String> list = new ArrayList<>();
            for (int i = 44; i >= 28; i--) {
                list.add(i + "周");
            }
            setList(list);
        }

        private void refresh(int position) {
            this.position = position;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, String itemsBean) {
            TextView item_gestational_name = holder.getView(R.id.item_gestational_name);
            holder.setText(R.id.item_gestational_name, itemsBean);
            if (holder.getAdapterPosition() == position) {
                item_gestational_name.setTextColor(ColorUtils.getColor(R.color.colorPrimary));
                int size = SizeUtils.px2sp(getContext().getResources().getDimension(R.dimen.base14sp));
                item_gestational_name.setTextSize(size);
                TextPaint paint = item_gestational_name.getPaint();
                paint.setFakeBoldText(true);
            } else {
                int size = SizeUtils.px2sp(getContext().getResources().getDimension(R.dimen.base13sp));
                item_gestational_name.setTextSize(size);
                item_gestational_name.setTextColor(ColorUtils.getColor(R.color.colorGray));
                TextPaint paint = item_gestational_name.getPaint();
                paint.setFakeBoldText(false);
            }
        }
    }

    public static class DayAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public DayAdapter() {
            super(R.layout.item_gestational);
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= 6; i++) {
                list.add(i + "天");
            }
            setList(list);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, String itemsBean) {
            holder.setText(R.id.item_gestational_name, itemsBean);
        }
    }
}